package net.dnw.practicesummary.home.presenter;


import android.util.Log;

import com.google.gson.Gson;

import net.dnw.practicesummary.home.contract.MainContract;
import net.dnw.practicesummary.home.entity.DataBean;
import net.dnw.practicesummary.mvp.net.HttpResponse;
import net.dnw.practicesummary.mvp.base.BasePresenter;
import net.dnw.practicesummary.mvp.net.BaseDisposableObserver;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by Administrator on 2019/2/22 0022.
 */

public class MainPresenterImpl extends BasePresenter<MainContract.MainModel, MainContract.MainView> implements MainContract.MainPresenter {


    @Override
    public void getData() {//这里要注意判空（view和model可能为空）

        model.getDataFromNet()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseDisposableObserver<HttpResponse>() {
                    @Override
                    public void responseResult(HttpResponse result) {
                        try {
                            JSONArray array = new JSONArray(result.getData().toString());
                            List<DataBean> dataBeans = new ArrayList<>();
                            DataBean dataBean;
                            Gson gson = new Gson();
                            for (int i = 0; i < array.length(); i++) {
                                dataBean = gson.fromJson(array.getJSONObject(i).toString(), DataBean.class);
                                dataBeans.add(dataBean);
                            }
                            Log.e("responseResult", "" + dataBeans.get(0));
                            getView().setTestTip(dataBeans.get(0).getName() + " ---------- " + dataBeans.get(0).getAddress());

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void errorResult(Throwable e, String errorMsg) {
                        Log.e("errorResult:", "" + errorMsg);
                    }
                });

        //测试map 转换处理请求
      /*  model.getDataFromNet()
                .map(new Function<String, HashMap<String,String>>() {
                    @Override
                    public HashMap<String,String> apply(String s) throws Exception {
                        HashMap<String,String> map = new HashMap<>();
                        map.put("key",s);
                        return map;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HashMap<String,String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        d.isDisposed();
                        d.dispose();
                    }

                    @Override
                    public void onNext(HashMap<String,String> data) {
                        String key = data.get("key");
                        getView().setTestTip(key);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });*/

        //测试嵌套请求  flatMap
       /* model.getDataFromNet()
                .flatMap(new Function<String, ObservableSource<String>>() {
                    @Override
                    public ObservableSource<String> apply(String s) throws Exception {
                        return model.getDataFromNet();
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });*/


    }

    @Override
    public void onViewDestroy() {

    }
}
